<template>
	<cl-crud ref="Crud">
		<el-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 权限 -->
			<cl-filter label="权限">
				<cl-select :options="options.vip" prop="vip" style="width: 120px" />
			</cl-filter>
			<!-- 状态 -->
			<cl-filter label="状态">
				<cl-select :options="options.status" prop="status" style="width: 120px" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key />
		</el-row>

		<el-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</el-row>

		<el-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</el-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="app-menu" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { reactive } from "vue";

const { service } = useCool();

// 数据选项
const options = reactive({
	vip: [
		{
			label: "普通",
			value: 0,
			type: "danger"
		},
		{
			label: "会员",
			value: 1,
			type: "success"
		}
	],
	status: [
		{
			label: "禁用",
			value: 0,
			type: "danger"
		},
		{
			label: "正常",
			value: 1,
			type: "success"
		}
	]
});

// cl-upsert 配置
const Upsert = useUpsert({
	items: [
		{ label: "菜单图标", prop: "icon", component: { name: "cl-upload" }, required: true },
		{ label: "菜单名称", prop: "name", span: 12, required: true, component: { name: "el-input" } },
		{
			label: "是否登录",
			prop: "login",
			span: 12,
			required: true,
			component: {
				name: "el-select",
				options: [
					{ label: "是", value: 1 },
					{ label: "否", value: 0 }
				]
			}
		},
		{
			label: "跳转路径",
			prop: "path",
			span: 12,
			required: true,
			component: {
				name: "el-input",
				props: {
					placeholder: "pages/index/index?id=1"
				}
			}
		},
		{
			label: "学生认证",
			prop: "stuauth",
			span: 12,
			required: true,
			component: {
				name: "el-select",
				options: [
					{ label: "是", value: 1 },
					{ label: "否", value: 0 }
				]
			}
		},
		{ label: "菜单标签", prop: "badge", span: 12, component: { name: "el-input" } },
		{
			label: "用户权限",
			prop: "vip",
			span: 12,
			required: true,
			component: {
				name: "el-select",
				options: [
					{ label: "会员", value: 1 },
					{ label: "普通", value: 0 }
				]
			}
		},
		{ label: "小程序ID", prop: "appId", span: 12, component: { name: "el-input" } },
		{
			label: "跳转类型",
			prop: "type",
			span: 12,
			value: 'nav',
			required: true,
			component: {
				name: "el-select",
				options: [
					{ label: "页面", value: "nav" },
					{ label: "程序", value: "mp" }
				]
			}
		},
		{
			label: "菜单排序",
			prop: "sort",
			span: 12,
			required: true,
			component: {
				name: "el-input-number",
			}
		},
		{
			label: "菜单状态",
			prop: "status",
			span: 12,
			value: 1,
			required: true,
			component: {
				name: "el-select",
				options: [
					{ label: "启用", value: 1 },
					{ label: "禁用", value: 0 }
				]
			}
		}
	]
});

// cl-table 配置
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "ID", prop: "id", width: 50 },
		{ label: "图标", prop: "icon", width: 100, component: { name: "cl-image", props: { size: 60 } } },
		{ label: "名称", prop: "name" },
		{ label: "路径", prop: "path" },
		{ label: "标签", prop: "badge", width: 80 },
		{
			label: "排序",
			prop: "sort",
			sortable: true,
		},
		{
			label: "用户权限",
			prop: "vip",
			dict: [
				{ label: "普通", value: 0, type: "danger" },
				{ label: "会员", value: 1, type: "success" }
			]
		},
		{
			label: "状态",
			prop: "status",
			dict: [
				{ label: "禁用", value: 0, type: "danger" },
				{ label: "正常", value: 1, type: "success" }
			]
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud 配置
const Crud = useCrud(
	{
		service: service.app.menu
	},
	(app) => {
		app.refresh();
	}
);
</script>